//package com.yq.cp.exceptionHandler;
//
//import com.alibaba.fastjson.JSONObject;
//import com.google.common.base.Charsets;
//import com.yq.cp.config.MyException;
//import com.yq.cp.entity.common.ApiResult;
//import org.reactivestreams.Publisher;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.core.io.buffer.DataBufferFactory;
//import org.springframework.core.io.buffer.DataBufferUtils;
//import org.springframework.core.io.buffer.DefaultDataBufferFactory;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//
//
//@Component
//public class ExceptionFilter implements Ordered, GlobalFilter {
//
//    /**
//     * 如果需要拦截返回值并处理，这里则需要返回负数
//     */
//    @Override
//    public int getOrder() {
//        return -1;
//    }
//
//    /**
//     * 异常拦截，拦截响应值不是 200 的数据，并抛给全局异常处理
//     */
//    @Override
//    @SuppressWarnings({"unchecked", "NullableProblems"})
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        ServerHttpResponse originalResponse = exchange.getResponse();
//        DataBufferFactory bufferFactory = originalResponse.bufferFactory();
//        ServerHttpResponseDecorator response = new ServerHttpResponseDecorator(originalResponse) {
//            @Override
//            public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
//                if (!getStatusCode().equals(HttpStatus.OK) && body instanceof Flux) {
//                    Flux<? extends DataBuffer> fluxBody = Flux.from(body);
//                    return super.writeWith(fluxBody.buffer().map(dataBuffers -> {
//                        DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
//                        DataBuffer join = dataBufferFactory.join(dataBuffers);
//                        byte[] content = new byte[join.readableByteCount()];
//                        join.read(content);
//                        DataBufferUtils.release(join);
//                        // 流转为字符串
//                        String responseData = new String(content, Charsets.UTF_8);
//                        ApiResult apiResult = JSONObject.parseObject(responseData, ApiResult.class);
//                        if (apiResult.getCode()==null || apiResult.getCode()!=200){
//                            throw new MyException(apiResult.getCode(),apiResult.getMsg());
//                        }
//                        byte[] uppedContent = responseData.getBytes(Charsets.UTF_8);
//                        originalResponse.getHeaders().setContentLength(uppedContent.length);
//                        return bufferFactory.wrap(uppedContent);
//                    }));
//                }
//                return super.writeWith(body);
//            }
//
//            @Override
//            public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
//                return writeWith(Flux.from(body).flatMapSequential(p -> p));
//            }
//        };
//        return chain.filter(exchange.mutate().response(response).build());
//    }
//
//}